/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package imat;


import java.awt.CardLayout;
import javax.swing.ImageIcon;
import se.chalmers.ait.dat215.project.Product;
import se.chalmers.ait.dat215.project.ShoppingCart;
import se.chalmers.ait.dat215.project.ShoppingItem;
/**
 *
 * @author Gustav
 */
public class CheckoutPresenter {
    private CheckoutView checkoutView;

    public CheckoutPresenter (CheckoutView view) {
        checkoutView = view;
    }

    public void updateReceipt() {
        ShoppingCart cart = DataHandler.getInstance().getShoppingCart();
        checkoutView.getReceipt().setText("");
        for (ShoppingItem item : cart.getItems()) {
            Product p = item.getProduct();
            //checkoutView.getReceipt().append(p.getName() + "\t" +item.getAmount()+" "+ p.getUnitSuffix()+
            //        " * "+ p.getPrice()+"\t\t" + item.getTotal()+" kr\n");

            String price = item.getAmount() +" "+ p.getUnitSuffix()+" * "+p.getPrice();
            checkoutView.getReceipt().append(fillOut(p.getName(), 20) + 
                    fillOut(price, 20) +
                    fillOut(Double.toString(item.getTotal())+" kr", 0)+"\n");
        }
        checkoutView.getReceipt().append("\n\n\n" +
                fillOut("", 20) + fillOut("Totalt:", 20) + cart.getTotal() + " kr");
    }

    public String fillOut(String in, int n) {
        StringBuilder out = new StringBuilder(in);
        for (int i=0; i<n-in.length(); i++) {
            out.append(' ');
        }
        return out.toString();
    }

    public void payButtonPressed() {
        CardLayout cl = (CardLayout)(DataHandler.getInstance().getMainWindow().getViewPanel().getLayout());
        cl.show(DataHandler.getInstance().getMainWindow().getViewPanel(), "payCard");
        DataHandler.getInstance().hideSideTrolley();
    }

    void backButtonPressed() {
        CardLayout cl = (CardLayout)(DataHandler.getInstance().getMainWindow().getViewPanel().getLayout());
        cl.show(DataHandler.getInstance().getMainWindow().getViewPanel(), "homeCard");
        DataHandler.getInstance().getMainWindow().loadSubMenuHome("");
        DataHandler.getInstance().getMainWindow().getHomeView().getjLabel1().setIcon(new ImageIcon("images/pierre_forsta1.png"));
    }

}
